/* ATMImpl.java
 *
 * Author:	Malcolm Staudacher
 * Date:	Nov 1, 2008
 *
 */

package fhv.itb.ec.atm;

import java.util.HashMap;

public class ATMImpl extends ATMPOA {

	private static HashMap<Integer, Account> m_accts;


	public ATMImpl() {

		// make sure that accounts are available
		if (m_accts == null) {
			m_accts = new HashMap<Integer, Account>();
			m_accts.put(new Integer(1), new Account(1, 0));
			m_accts.put(new Integer(2), new Account(2, 100));
			m_accts.put(new Integer(3), new Account(3, 500));
		}

	}


	public void deposit(int accountNo, float amount) {
		Account a = findAccount(accountNo);

		if (a != null) {
			synchronized (a) {
				a.deposit(amount);
			}
		} else {
			System.out.println("Account for deposit not found");
		}

	}


	public float getBalance(int accountNo) {
		Account a = findAccount(accountNo);

		if (a != null) {
			synchronized (a) {
				return a.getBalance();
			}
		} else {
			System.out.println("Account for balance inquiry not found");
		}

		return 0;
	}


	public void withdraw(int accountNo, float amount) {
		Account a = findAccount(accountNo);

		if (a != null) {
			synchronized (a) {
				a.withdraw(amount);
			}
		} else {
			System.out.println("Account for withdrawal not found");
		}

	}


	private Account findAccount(int number) {
		return m_accts.get(new Integer(number));
	}

}
